import os.path
import shutil
import re
import os
# 遍历指定目录，显示目录下的所有文件名
def eachFile(filepath,imagespath,savepath):
    i=0
    pathDir = os.listdir(filepath)
    for allDir in pathDir:
        # child = os.path.join('%s\%s' % (filepath, allDir))
        # print(allDir[:-4])
        print(allDir.split('.')[0])
        getpath=allDir.split('.')[0]+'.jpg'
        # child = os.path.join(filepath, getpath)
        # if os.path.isfile(child):
        #     print(child)
        child = os.path.join(imagespath, getpath)
        if os.path.isfile(child):
            print(child)
            shutil.copy(child, savepath)
            i=i+1
        #     readFile(child) 
        #     continue
        # eachFile(child)
    print('总数',i)

# 遍历出结果 返回文件的名字
# def readFile(filenames):
#     f = open(filenames, 'r')  # r 代表read
#     line = f.readline()  # 以行的形式进行读取文件
#     print('line',line)
#     list1 = []
#     while line:
#         a = line.split()
#         str = a[0]   # 读取该行的第一个数据
#         if str == '2':   #可修改标签
#             list1.append(filenames)
#             # shutil.copy(filenames, './labels0')
#             shutil.copy(filenames, '/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset3/coco5/chair/labels')
#             print("匹配到:" + filenames)
#         line = f.readline()
#     f.close()

# import sys
# sys.argv[0] 表示程序自身
# sys.argv[1] 表示程序的第一个参数
# sys.argv[2] 表示程序的第二个参数


if __name__ == "__main__":
    # filenames = './label10'  #标签根目录
    # filenames = '/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset3/coco5/all/bicycle/labels'  #标签根目录
    # imagespath='/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset3/coco_test/image10'
    # imagespath='/dssg/home/zhineng_qt/test_2021_3_18_fu/final_project/dataset/test/coco-20/images/train/images'
    
    # savepath='/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset3/coco5/all/bicycle/images'

   
    # imagespath='/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset3/coco_test/image10'
    # imagespath='/dssg/home/zhineng_qt/test_2021_3_18_fu/final_project/dataset/test/coco-20/images/train/images'
    # filenames = '/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset3/coco5/all2/bicycle/labels'  #标签根目录
    # savepath='/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset3/coco5/all2/bicycle/images'

    # filenames = '/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset3/coco5/all2/car/labels'  #标签根目录
    # savepath='/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset3/coco5/all2/car/images'
    path_name_back='diningtable'
    imagespath='/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset2/voc_5c/images'
    filenames = '/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset4_co_vo/split_voc_class/{}/labels'.format(path_name_back)  #标签根目录
    savepath='/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset4_co_vo/split_voc_class/{}/images'.format(path_name_back)
    if not os.path.exists(savepath):
        os.makedirs(savepath)
    arr = []
    eachFile(filenames,imagespath,savepath)

# classes = ['chair','bicycle','car','motorbike','diningtable']   label_get_image.py